<?php

class Customer
{
	private $user_name;
	private $user_passwd;
	private $_table;
	private $db;
	
	// constructor
	public function __CONSTRUCT()
	{
		$this->db=MyDB::$db;
		//$this->db->debug = true;
		$this->_table = 'user';
	}
	public function validate($user_name,$user_passwd)
	{
		$this->user_name=$user_name;
		$this->user_passwd=$user_passwd;
		return $this->check_login();
	}
	// end constructor
	//check authentication Admin
	private function check_login()
	{
		$record=$this->db->Execute("select * from ".$this->_table." where binary email='".$this->user_name."' and  binary password='".$this->user_passwd."' and status=1");
		if($this->db->ErrorNo() == 0 )
		{
			if( $record->RecordCount() > 0)
			{
				$_SESSION[_USER_ID]= $record->fields['id'];
				$_SESSION[_USER_NAME]= $record->fields['email'];
				$_SESSION[_NAME]= $record->fields['title'].' '.$record->fields['first_name'].' '.$record->fields['last_name'];
				return true;
			}
		}
		return false;
	}
	public function get_password($username) {
	
		$this->user_name = $username;
		$record=$this->db->GetRow("select * from ".$this->_table." where binary email='".$this->user_name."' and status=1");
		if($this->db->ErrorNo() == 0 )
		{
			return $record;
		}
		return false;
	}
	//end Authentication admin
	
	
	public function customer_registration($arr, $id = NULL) {
	
		if($id != NULL) {
			$sql = "UPDATE ";
			$where = " ,modify_date=now() WHERE id = ".$id;
		}
		else
			$sql = "INSERT INTO ";
			
		$sql.= $this->_table." SET name= '".$arr['name']."',email= '".$arr['email']."',password= '".$arr['password']."',created_on= now(),mobile= '".$arr['mobile']."'".$where;
		if($this->db->Execute($sql))
			if($id == NULL)
				return $this->db->Insert_Id();
			else
				return true;
		return false;
	}
	public function check_email($email='') {
		$res = $this->db->Execute("SELECT * FROM ".$this->_table." WHERE email='".$email."'");
		if($res->RecordCount())
			return false;
		return true;
	}
	
	public function get_profile() {
		
		$sql = "SELECT * FROM ".$this->_table." WHERE id = ".$this->_id;
		$this->profile_arr = $this->db->GetRow($sql);
		if((is_array($profile_arr)) && ($this->db->ErrorNo() == 0)){
			$this->set_profile();
		}
	}
	//Logout User
	public function logout()
	{
		unset($_SESSION);
		session_destroy();
	}
	
	public function customer_contact($arr) {
		$this->_table = 'contactus';
		$sql = " INSERT INTO ".$this->_table." SET user_id='".(int)$this->_id."',name ='".$arr['name']."',phone ='".$arr['phone']."',email ='".$arr['email']."',query='".$arr['query']."'";
		if($this->db->Execute($sql))
			return true;
		return false;
	}
}
?>